import mysql.connector
from mysql.connector import Error
import logging
logging.basicConfig(level=logging.DEBUG)

def test_db_connection():
    try:
        # 替换为你的数据库配置
        db_config = {
            'host': '1.1.1.1',
            'user': 'root',
            'password': '********',
            'database': 'student_db',
            'charset': 'utf8mb4',
            'collation': 'utf8mb4_unicode_ci',
            'port': 3306,
            'use_pure': True  # 强制使用纯 Python 模式
        }

        # 创建数据库连接
        connection = mysql.connector.connect(**db_config)

        if connection.is_connected():
            print("Successfully connected to the database!")
            cursor = connection.cursor()
            cursor.execute("SELECT VERSION()")
            record = cursor.fetchone()
            print(f"You're connected to MySQL version: {record[0]}")

            # 关闭游标和连接
            cursor.close()
            connection.close()
            print("MySQL connection is closed.")

    except Error as e:
        print(f"Error while connecting to MySQL: {e}")

if __name__ == "__main__":
    test_db_connection()